/*
 *      dxtabitem.cpp
 *
 *      Copyright 2010 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#endif
#include "dxtabitem.h"
#include "tetristabitem.h"
#include "icons.h"
#include "config.h"
#include "i18n.h"
#include "utils.h"

FXDEFMAP(dxTabItem) dxTabItemMap[] = {
    FXMAPFUNC(SEL_TEXTLINK,             dxTabItem_TEXT,     dxTabItem::onTextLink),
    FXMAPFUNC(SEL_RIGHTBUTTONRELEASE,   dxTabItem_TEXT,     dxTabItem::onTextRightMouse),
    FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE,  dxTabItem_TEXT,     dxTabItem::onTextMiddleMouse)
};

FXIMPLEMENT(dxTabItem, dxEXTabItem, dxTabItemMap, ARRAYNUMBER(dxTabItemMap))

dxTabItem::dxTabItem(dxTabBook *tab, const FXString &tabtext, FXIcon *icon, FXuint opts,
        FXint id)
    : dxEXTabItem(tab, tabtext, icon, opts), m_id(id)
{
    this->setIconPosition(ICON_BEFORE_TEXT);

    m_text = new dxText(tab, this, dxTabItem_TEXT, FRAME_SUNKEN|LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_READONLY|TEXT_WORDWRAP|TEXT_SHOWACTIVE|TEXT_AUTOSCROLL);
}

dxTabItem::~dxTabItem()
{
}

long dxTabItem::onTextLink(FXObject *, FXSelector, void *data)
{
    utils::launchLink(static_cast<FXchar*>(data));
    return 1;
}

long dxTabItem::onTextRightMouse(FXObject *, FXSelector, void *ptr)
{
    FXEvent* event=(FXEvent*)ptr;
    if(event->moved) return 1;
    FXMenuPane popupmenu(this);
    FXMenuCommand *clear = new FXMenuCommand(&popupmenu, _("Clear window"), ICO_CLEAR, m_text, dxText::ID_CLEAR);
    new FXMenuSeparator(&popupmenu);
    FXMenuCommand *selectall = new FXMenuCommand(&popupmenu, _("Select All"), ICO_SELECTALL, m_text, dxText::ID_SELECT_ALL);
    FXMenuCommand *copy = new FXMenuCommand(&popupmenu, _("Copy"), ICO_COPY, m_text, dxText::ID_COPY_SEL);
    new FXMenuSeparator(&popupmenu);
    FXMenuCommand *find = new FXMenuCommand(&popupmenu, _("Find"), ICO_FIND, m_text, dxText::ID_SEARCH);
    if(!m_text->hasSomeText())
    {
        clear->disable();
        selectall->disable();
        copy->disable();
        find->disable();
    }
    if(!m_text->hasSelectedText())
    {
        copy->disable();
    }
    popupmenu.create();
    popupmenu.popup(NULL,event->root_x,event->root_y);
    getApp()->runModalWhileShown(&popupmenu);
    return 1;
}

long dxTabItem::onTextMiddleMouse(FXObject*, FXSelector, void *ptr)
{
    FXEvent* event=(FXEvent*)ptr;
    if(event->moved) return 1;
#ifndef WIN32 //middleclick copy
    m_text->copySelectedText();
#endif
    return 1;
}
